package DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import GUI.Gestion.Tabla;
//import GUI.almacen.TablaAlmacen.Datos;

public class DAO {
	
	Conexion cn;
	
	public DAO(){}
	
	public void conectar(){
		cn=new Conexion();
	}
	
	public Connection getBD(){
		return cn.AccederBD();
	}
	
	

	public void insert(String table, String fields, String values,Connection con) 
    {
      //  boolean res=false;
        //Se arma la consulta
        String q=" INSERT INTO " + table + " ( " + fields + " ) VALUES ( " + values + " ) ";
        //se ejecuta la consulta
        try {
            PreparedStatement pstm = con.prepareStatement(q);
            pstm.execute();
            pstm.close();
           
            
           JOptionPane.showMessageDialog(null,"Insertado correctamente");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage());
        }
        
    
    }
	
	//ALTER TABLE cliente MODIFY nombre_cliente NULL
	
	public void update(String clave,String table, String column,String values,Connection con) 
    {
      
        //Se arma la consulta
        String q=" UPDATE " + table + " SET " + column + " = " + values + " where dni="+clave;
        //se ejecuta la consulta
        try {
            PreparedStatement pstm = con.prepareStatement(q);
            pstm.execute();
            pstm.close();
           
            
           JOptionPane.showMessageDialog(null,"Modificado correctamente");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage());
        }
        
    
    }
	
	/**
	public String [][] select(String table, String fields, String where,Connection con){
	      int registros = 0;      
	      String colname[] = fields.split(",");

	      //Consultas SQL
	      String q ="SELECT " + fields + " FROM " + table;
	      String q2 = "SELECT count(*) as total FROM " + table;
	      if(where!=null)
	      {
	          q+= " WHERE " + where;
	          q2+= " WHERE " + where;
	      }
	       try{
	         PreparedStatement pstm = con.prepareStatement(q2);
	         ResultSet res = pstm.executeQuery();
	         res.next();
	         registros = res.getInt("total");
	         res.close();
	      }catch(SQLException e){
	         JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
	      }
	    
	    //se crea una matriz con tantas filas y columnas que necesite
	    String[][] data = new String[registros][fields.split(",").length];
	    //realizamos la consulta sql y llenamos los datos en la matriz "Object"
	      try{
	         PreparedStatement pstm = con.prepareStatement(q);
	         ResultSet res = pstm.executeQuery();
	         int i = 0;
	         while(res.next()){
	            for(int j=0; j<=fields.split(",").length-1;j++){
	                data[i][j] = res.getString( colname[j].trim() );
	            }
	            i++;         }
	         res.close();
	          }catch(SQLException e){
	         System.out.println(e);
	    }
	    return data;
	 }
	**/
	/**
	public String[][] getClientes(Connection con)
    {
        String[][] res = this.select("empleado", "dni,pass,nombre,rol,direccion,telefono", null,con);
        if( res.length > 0)
            return res;
        else
            return null;
    }**/
	
	
	
	public String getID(String s,Connection con){
		String st=null;
		Statement statement;
		try{
		statement=con.createStatement();
		ResultSet rs=statement.executeQuery("select dni,rol from empleado");
		ResultSetMetaData metadata=rs.getMetaData();
		int col=metadata.getColumnCount();
		
		while(rs.next()){
			for(int j=1;j<=col;j++){
				if(rs.getString(j).equalsIgnoreCase(s))
					st=rs.getString(j+1);
			
			}
		}
		}catch(SQLException e){e.printStackTrace();}
		
	   return st;	
	}
	
	
	public void muestraTabla(String s,String[] ss,Tabla t,Connection c){
	Statement statement;
	try{
	statement=c.createStatement();
	ResultSet rs=statement.executeQuery(s);
	ResultSetMetaData metadata=rs.getMetaData();
	int col=metadata.getColumnCount();

	
	while(rs.next()){
		 
		  int i=0;
			for(int j=1;j<=col;j++){
				ss[i]=rs.getString(j);
				i++;
			}
			t.addRow(ss);		
	}
	}catch(SQLException e){e.printStackTrace();}
	}
	
	//////////////////////////////////////////almacen///////////////////////////
	
	
	  public void buscaArt(String s,Object[] ss,DefaultTableModel m,Connection c)  {
	        
	        Statement statement;
			try{
			statement=c.createStatement();
			
			ResultSet rs=statement.executeQuery(s);
			ResultSetMetaData metadata=rs.getMetaData();
			int col=metadata.getColumnCount();
			
			while(rs.next()){
				int i=0;
				for(int j=1;j<=col;j++){
					
						ss[i]=rs.getString(j);
						
						i++;
				}
				m.addRow(ss);
			}
			}catch(SQLException e){e.printStackTrace();}
	}
	  
	 ///////////////////////////////////////////Ventas//////////////////////////////////
	  
	
}
